package com.societyGames.faceExercises
{
import com.societyGames.action.ActionQueue;
import com.societyGames.flashForms.AutoScaleTextField;

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.Dictionary;

public class FaceRender extends Sprite
{
  private var _faceSimulation:FaceSimulation;
  private var _actionQueue:ActionQueue;
  private var _faceDataRenders:Dictionary = new Dictionary(); //name:String to FaceDataRender
  private var _track:Sprite = new Sprite();
  private var _whoTextField:TextField;

  private static const SPACING_TEXT_TO_FACES:Number = 10;

  public function FaceRender(faceSimulation:FaceSimulation, actionQueue:ActionQueue)
  {
    this._faceSimulation = faceSimulation;
    this._actionQueue = actionQueue;
  }

  //Create the graphics from the data in the simulation
  public function initialize():void
  {
    var y:Number = 0;

    this._whoTextField = createWhoTextField();
    this._whoTextField.text = this._faceSimulation.description;
    this.addChild(this._whoTextField);

    y += this._whoTextField.height;
    y += SPACING_TEXT_TO_FACES;

    //Track
    var x:Number = 0;
    for each (var faceData:FaceData in this._faceSimulation.people)
    {
      var faceDataRender:FaceDataRender = new FaceDataRender(faceData, true);
      this._faceDataRenders[faceData.name] = faceDataRender;
      faceDataRender.x = x;
      x += faceDataRender.width;
      this._track.addChild(faceDataRender);
    }
    this._track.x = 0;
    this._track.y = y;
    this.addChild(this._track);

    this._whoTextField.x = (x - this._whoTextField.textWidth) / 2;
  }

  public function createWhoTextField():TextField
  {
    var whoTextField:TextField = new AutoScaleTextField();
    //whoTextField.border = true;
    //whoTextField.borderColor = 0x000000;
    whoTextField.autoSize = TextFieldAutoSize.CENTER;
    whoTextField.width = 200;
    whoTextField.height = 100;
    return whoTextField;
  }

  public function pointAt(name:String, showSad:Boolean):void
  {
    var faceDataRender:FaceDataRender = this._faceDataRenders[name];
    this._actionQueue.add(new PointAtFaceAction(faceDataRender, showSad, 0.5));
  }

  public function stopPointAt(name:String):void
  {
    var faceDataRender:FaceDataRender = this._faceDataRenders[name];
    this._actionQueue.add(new StopPointAtFaceAction(faceDataRender, 0.5));
  }
}
}